package com.cyf.plc.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.cyf.plc.databinding.ItemFloorBinding
import com.cyf.plc.model.FloorWithRooms
import com.cyf.plc.viewholder.FloorViewHolder

class FloorAdapter(private val onItemClickListener: OnItemClickListener) : ListAdapter<FloorWithRooms, FloorViewHolder>(DIFF_CALLBACK) {

    interface OnItemClickListener {
        fun onItemClicked(plcFloor: FloorWithRooms)
        fun onItemLongClick(plcFloor: FloorWithRooms)
    }

    companion object {
        private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<FloorWithRooms>() {

            override fun areItemsTheSame(
                oldItem: FloorWithRooms,
                newItem: FloorWithRooms
            ): Boolean =
                oldItem.plcFloor.floorId == newItem.plcFloor.floorId

            override fun areContentsTheSame(
                oldItem: FloorWithRooms,
                newItem: FloorWithRooms
            ): Boolean =
                oldItem == newItem
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = FloorViewHolder(
        ItemFloorBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )

    override fun onBindViewHolder(holder: FloorViewHolder, position: Int) {
        holder.bind(getItem(position), onItemClickListener)
    }
}